package ver0.util;

public class MDGauss {
	
	public static double gauss1D(double sigma,int x){
		double index=-x*x/(2*sigma*sigma);
		double denominator=Math.sqrt(2*Math.PI)*sigma;
		return Math.exp(index)/denominator;
	}
	
	public static double gauss2D(double sigma,int x,int y){
		double index=-(x*x+y*y)/(2*sigma*sigma);
		double denominator=2*sigma*sigma*Math.PI;
		return Math.exp(index)/denominator;
	}
	
	public static double[] get1DArray(double sigma,int radius){
		double[] array=new double[2*radius+1];
		for(int i=0;i<2*radius+1;i++){
			int distance=i-radius<0?radius-i:i-radius;
			array[i]=gauss1D(sigma,distance);
		}
		return array;
	}
	
	public static double[][] get2DMatrix(double sigma,int radius){
		double[][] matrix=new double[2*radius+1][2*radius+1];
		for(int x=0;x<2*radius+1;x++){
			for(int y=0;y<2*radius+1;y++){
				int distanceX=x-radius<0?radius-x:x-radius;
				int distanceY=y-radius<0?radius-y:y-radius;
				matrix[x][y]=gauss2D(sigma,distanceX,distanceY);
			}
		}
		return matrix;
	}
	
	public static double[] normalize1D(double[] array){
		double sum=0;
		for(int i=0;i<array.length;i++){
			sum+=array[i];
		}
		for(int i=0;i<array.length;i++){
			array[i]/=sum;
		}
		return array;
	}
	
	public static double[][] normalize2D(double[][] matrix){
		double sum=0;
		for(int i=0;i<matrix.length;i++){
			for(int j=0;j<matrix[i].length;j++){
				sum+=matrix[i][j];
			}
		}
		for(int i=0;i<matrix.length;i++){
			for(int j=0;j<matrix[i].length;j++){
				matrix[i][j]/=sum;
			}
		}
		return matrix;
	}

}
